//生成账目任务(steps步骤数组，job步骤名称，djid单据id，gs公司，wllx往来类型，wldx往来对象，zklx账款类型，zkje账款金额，yjje已结金额，zt状态，ywsj业务时间，ywdj业务单据，xm项目，ywlx业务类型，bsc办事处,wlxz往来性质)
function dr_zmrw(steps,job,djid,gs,wllx,wldx,zklx,zkje,yjje,zt,ywsj,ywdj,xm,ywlx,bsc,wlxz){
    var rw={
        "step_name": job,
        "opt_type": 1,
        "datatable_id": "1886323389442822144",
        "args": "",
        "field_params": [{
            "value_from": 2,
            "field": "djid",
            "value": djid,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "gs",
            "value": gs,
            "data_type": 1
        },{
          "value_from": 1,
          "field": "wllx",
          "value": wllx,
          "data_type": 1
      },{
            "value_from": 1,
            "field": "wldx",
            "value": wldx,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "zklx",
            "value": zklx,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "zkje",
            "value": zkje,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "yjje",
            "value": yjje,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "zt",
            "value": zt,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "ywsj",
            "value": ywsj,
            "data_type": 1
        },{
          "value_from": 1,
          "field": "ywdj",
          "value": ywdj,
          "data_type": 1
        },{
          "value_from": 1,
          "field": "xm",
          "value": xm,
          "data_type": 1
        },{
          "value_from": 1,
          "field": "ywlx",
          "value": ywlx,
          "data_type": 1
        },{
          "value_from": 1,
          "field": "bsc",
          "value": bsc,
          "data_type": 1
        },{
          "value_from": 1,
          "field": "wlxz",
          "value": wlxz,
          "data_type": 1
        }]
    }
    steps.push(rw);
    //steps=jy_xgwlje(steps,job,xm,gs,bsc,wldx,zklx,zkje)
    return steps;
  }
  //创建往来余额并执行事务
  function dr_cjwlye(argsobj,xm,gs,bsc,wldxs,success,fails){
    debugger
    if(wldxs){
      var args={"steps":[]}
      var cond="a.:[xm]=? and a.:[gs]=? and a.:[bsc]=? and (";
      var values=[xm,gs,bsc];
      for(var i=0;i<wldxs.length;i++){
        cond+="(a.:[wldx]=? and a.:[zklx]=?)";
        if(i!=wldxs.length-1){
          cond+=" or ";
        }else{
          cond+=")";
        }
        values.push(wldxs[i].wldx);
        values.push(wldxs[i].zklx);
      }
      //查询往来对象余额是否存在，不存在则需要新建
      var args2 = {
        "datatable_id": "1914534321918578688",
        "search": "",
        "search_fields": [
          {
            "quick_search": true,
            "field": "wldx",
            "table": "a",
            "data_type": 1
          },
          {
            "quick_search": true,
            "field": "zklx",
            "table": "a",
            "data_type": 1
          }
        ],
        "advance_filters": {condition: cond, values: JSON.stringify(values)},
        "page": 1,
        "page_size": 20,
        "include": {} 
      }
      ycWindow.plugins.crud.Query(args2, (result) => {
        for(var i=0;i<wldxs.length;i++){
          //当前往来余额是否存在
          var exsit=false;
          for(var j=0;j<result.total;j++){
            if(wldxs[i].wldx==result.data[j].wldx&&wldxs[i].zklx==result.data[j].zklx){
              exsit=true;
            }
          }
          if(!exsit){
              args.steps.push({
                "step_name": "插入往来余额"+i,
                "opt_type": 1,
                "datatable_id": "1914534321918578688",
                "args": "",
                "field_params": [{
                    "value_from": 1,
                    "field": "xm",
                    "value": xm,
                    "data_type": 1
                  },{
                    "value_from": 1,
                    "field": "gs",
                    "value": gs,
                    "data_type": 1
                  },{
                    "value_from": 1,
                    "field": "bsc",
                    "value": bsc,
                    "data_type": 1
                  },{
                    "value_from": 1,
                    "field": "wldx",
                    "value": wldxs[i].wldx,
                    "data_type": 1
                  },{
                    "value_from": 1,
                    "field": "zklx",
                    "value": wldxs[i].zklx,
                    "data_type": 1
                  },{
                    "value_from": 1,
                    "field": "zkye",
                    "value": 0,
                    "data_type": 1
                  },{
                    "value_from": 1,
                    "field": "wllx",
                    "value": wldxs[i].wllx,
                    "data_type": 1
                  }]
            })
          }
        }
        ycWindow.plugins.crud.ExecJobs(args, (result) => {
          debugger
          ycWindow.plugins.crud.ExecJobs(argsobj, (data) => {
            debugger
            success(data);
          }, (data) => {
            debugger
            fails(data);
          })
        }, (data) => {
        })
      }, (data) => {
      })
    }
  }
  //修改往来金额
  function dr_xgwlje(steps,job,xm,gs,bsc,wldx,zklx,je){
    //查询往来对象金额
    var rw1={
      "step_name": "查询往来对象金额_"+job,
      "opt_type": 5,
      "query_args": {
        "datatable_id": "1914534321918578688",
        "search_fields": [
          {
            "field": "cast((a.:[zkye] + ("+je+")) as decimal(12,2)) as bghye",
            "table": "a",
            "data_type": 11
          }
        ],
        "advance_filters": {
          "condition": "a.:[xm]=? and a.:[gs]=? and a.:[bsc]=? and a.:[wldx]=? and a.:[zklx]=?",
          "values": JSON.stringify([xm,gs,bsc,wldx,zklx])
        }
      },
      "query_into": "往来对象金额_"+job
    };
    steps.push(rw1);
    //更新往来对象金额
    var rw2={
      "step_name": "更新往来对象金额_"+job,
      "opt_type": 4,
      "datatable_id": "1914534321918578688",
      "args": "往来对象金额_"+job+".id",
      "args_from": 3,
      "field_params": [
        {
          "value_from": 3,
          "field": "zkye",
          "value": "往来对象金额_"+job+".bghye",
          "data_type": 1
        },
        {
            "value_from": 1,
            "field": "rld",
            "value": "",
            "data_type": 1
          }
      ]
    };
    steps.push(rw2);
    return steps;
  }
  //新增新建单据工作流(steps,jobname,流程编号,流程开始时间,流程结束时间,流程当前状态,下一步状态,已完成,关联单据编号)
  function dr_xzgzl(steps,jobname,workflow_id,start_time,end_time,status,next_status,completed,data_id){
    var rw={
        "step_name": jobname,
        "opt_type": 1,
        "datatable_id": "1924682269423833088",
        "args": "",
        "field_params": [{
            "value_from": 1,
            "field": "workflow_id",
            "value": workflow_id,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "start_time",
            "value": start_time,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "end_time",
            "value": end_time,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "status",
            "value": status,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "next_status",
            "value": next_status,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "completed",
            "value": completed,
            "data_type": 1
        },{
            "value_from": 2,
            "field": "data_id",
            "value": data_id,
            "data_type": 1
        }]
    }
    steps.push(rw);
    return steps;
  }
  //新增已有单据工作流(steps,jobname,流程编号,流程开始时间,流程结束时间,流程当前状态,下一步状态,已完成,关联单据编号)
  function dr_xzgzl_yy(steps,jobname,workflow_id,start_time,end_time,status,next_status,completed,data_id){
    var rw={
        "step_name": jobname,
        "opt_type": 1,
        "datatable_id": "1924682269423833088",
        "args": "",
        "field_params": [{
            "value_from": 1,
            "field": "workflow_id",
            "value": workflow_id,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "start_time",
            "value": start_time,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "end_time",
            "value": end_time,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "status",
            "value": status,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "next_status",
            "value": next_status,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "completed",
            "value": completed,
            "data_type": 1
        },{
            "value_from": 1,
            "field": "data_id",
            "value": data_id,
            "data_type": 1
        }]
    }
    steps.push(rw);
    return steps;
  }